package cn.highedu.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class ServerBootApplication {
    private ServerSocket serverSocket;

//    /**
//     * 服务器的启动方法
//     */
//    public void start(){
//        try {
//            serverSocket = new ServerSocket(8088);
//            System.out.println("等待客户端的请求");
//            Socket clientSocket = serverSocket.accept();
//            System.out.println("读取请求消息");
//            InputStream in = clientSocket.getInputStream();
//            int b;
//            //builder 缓存了每行字符
//            StringBuilder builder = new StringBuilder();
//            char previous = 0; //前一个字符
//            char current = 0;  //当前字符
//            while ((b=in.read())!=-1){
//                current = (char) b;
//                // \r 就是 CR 字符， \n就是 LF 字符
//                // 如果连续出现 CRLF 表示读取到行末尾
//                if (previous == '\r' && current == '\n'){
//                    //如果 builder 中没有字符，则说明读取到了空行
//                    if (builder.toString().isEmpty()){
//                        //读取到空行，就结束循环读取
//                        break;
//                    }else{
//                        //不是空行，就输出缓存的这行字符串
//                        System.out.println(builder);
//                        //输出以后，清空字符串builder
//                        builder.delete(0, builder.length());
//                    }
//                }else if (current != '\n' && current != '\r'){
//                    //将读取的字符拼接为字符串，不包括回车换行符号
//                    builder.append(current);
//                }
//                previous = current;
//            }
//            OutputStream out = clientSocket.getOutputStream();
//            String html = "<html>" +
//                    "<head>" +
//                    "<title>Demo</title>" +
//                    "</head>" +
//                    "<body>Hello World!</body>" +
//                    "</html>";
//            //按照UTF-8对网页进行编码
//            byte[] body = html.getBytes(StandardCharsets.UTF_8);
//            //发送状态行
//            out.write("HTTP/1.1 200 OK".getBytes(StandardCharsets.ISO_8859_1));
//            out.write('\r'); //CR
//            out.write('\n'); //LF
//            //发送响应头信息，Content-Type 内容类型  Content-Length 内容长度
//            out.write("Content-Type: text/html; charset=utf-8".getBytes(StandardCharsets.ISO_8859_1));
//            out.write('\r');
//            out.write('\n');
//            out.write(("Content-Length: " + body.length).getBytes(StandardCharsets.ISO_8859_1));
//            out.write('\r');
//            out.write('\n');
//            //发送一个空行
//            out.write('\r');
//            out.write('\n');
//            //发送响应正文（body）
//            out.write(body);
//            out.close();
//            in.close();
//            clientSocket.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    public void start(){
        try {
            serverSocket = new ServerSocket(8088);
            while (true) {
                System.out.println("等待连接");
                Socket clientSocket = serverSocket.accept();
                System.out.println("启动客户端处理线程");
                ClientHandler clientHandler = new ClientHandler(clientSocket);
                Thread thread = new Thread(clientHandler);
                thread.start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //调用start方法启动服务器
        ServerBootApplication application = new ServerBootApplication();
        application.start();
    }
}
